package cn.golxen.blog.controller;

import cn.golxen.blog.commons.utils.Result;
import cn.golxen.blog.commons.vo.AddLabelSetVo;
import cn.golxen.blog.commons.vo.GetLabelAndEssayCountVo;
import cn.golxen.blog.entity.Essay;
import cn.golxen.blog.entity.Label;
import cn.golxen.blog.exception.exceptionClass.AddLabelException;
import cn.golxen.blog.exception.exceptionClass.DeleteLabelException;
import cn.golxen.blog.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@RestController
@RequestMapping(value = "/label")
public class LabelController {
    @Autowired
    LabelService labelService;

    @PostMapping(value = "/admin/add")
    public Result addLabel(@RequestBody Label label){
        Integer integer= labelService.addLabel(label);
        if (integer!=0)
            return Result.successResult();
        else
            throw new AddLabelException();
    }

    @PostMapping(value = "/admin/addSet")
    public Result addLabelSet(@RequestBody AddLabelSetVo addLabelSetVo){
        Integer integer=labelService.addLabelSet(addLabelSetVo);
        return new Result(200,"成功添加"+integer+"条标签");
    }

    @PostMapping(value = "/admin/delete")
    public Result deleteLabel(@RequestBody Label label){
        Integer integer=labelService.deleteLabel(label);
        if (integer!=0)
            return Result.successResult();
        else
            throw new DeleteLabelException();
    }

    @GetMapping(value = "/get/all")
    public Result getLabelAll(){
        Set<GetLabelAndEssayCountVo> set= labelService.getLabelAll();
        return Result.successResult(set);
    }

    @GetMapping(value = "/get/bycategory/{category}")
    public Result getLabelByCategory(@PathVariable(value = "category")String category){
        Set<GetLabelAndEssayCountVo> set= labelService.getLabelByCategory(category);
        return Result.successResult(set);
    }

    @GetMapping(value = "/get/essaycount/{label}")
    public Result getEssayCountByLabel(@PathVariable(value = "label") String label){
        Integer integer= labelService.getEssayCountByLabel(label);
        return Result.successResult(integer);
    }

}
